package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.service.UserService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;


    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end){
        ArrayList<LocalDate> dateTime = new ArrayList<>();

        dateTime.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateTime.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateTime) {
            LocalDateTime minTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime maxTime = LocalDateTime.of(localDate, LocalTime.MAX);

            Map dateMap = new HashMap<>();
            dateMap.put("begin",minTime);
            dateMap.put("end",maxTime);
            dateMap.put("status", Orders.COMPLETED);
            Double turnover =orderMapper.totaPrice(dateMap);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }
        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateTime,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    @Override
    public UserReportVO userVoStatistics(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        ArrayList<Integer> newUserList = new ArrayList<>();
        ArrayList<Integer> totalUserList = new ArrayList<>();

        for (LocalDate localDate : dateList) {
            LocalDateTime minDate = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime maxDate = LocalDateTime.of(localDate, LocalTime.MAX);

            Map map = new HashMap();
            map.put("end",maxDate);

            // 用户总数量
            Integer totalPerson = userMapper.sumByMap(map);

            Map addNewMap = new HashMap();
            addNewMap.put("begin",minDate);
            // 每日新增
            Integer addNew = userMapper.sumByMap(map);
            newUserList.add(addNew);
            totalUserList.add(totalPerson);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .build();
    }


    @Override
    public OrderReportVO getOrderTotal(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> totalOrderList = new ArrayList();
        List<Integer> completeOrderList = new ArrayList();


        for (LocalDate date : dateList) {
            LocalDateTime beginDate = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endDate = LocalDateTime.of(date, LocalTime.MAX);
            // 查询订单总数
            Integer totalOrders = getOrderTotalByDate(beginDate, endDate, null);

            // 查询已支付订单总数
            Integer completeOrders = getOrderTotalByDate(beginDate, endDate, Orders.COMPLETED);
            totalOrderList.add(totalOrders);
            completeOrderList.add(completeOrders);
        }

        // 计算时间区域内订单总数
        Integer totalOrderCount = totalOrderList.stream().reduce(Integer::sum).get();
        // 计算时间区域内已支付订单总数
        Integer completeOrderCount = completeOrderList.stream().reduce(Integer::sum).get();

        // 计算时间区域订单完成率
        Double completeRate = 0.0;
        if (totalOrderCount != 0) {
            completeRate = (double) completeOrderCount / totalOrderCount ;
        }

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(completeOrderCount)
                .orderCompletionRate(completeRate)
                .orderCountList(StringUtils.join(totalOrderList, ","))
                .validOrderCountList(StringUtils.join(completeOrderList, ","))
                .build();
    }

    private Integer getOrderTotalByDate(LocalDateTime begin, LocalDateTime end,Integer status) {
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", status);
        return orderMapper.countByMap(map);
    }

    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginDate = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endDate = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginDate, endDate);
        List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");
        List<Integer> counts = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String countList = StringUtils.join(counts, ",");
        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(countList)
                .build();
    }
}
